<?php

abstract class Quad_Esc {

	public static function html($value) {
		$value = htmlentities($value, ENT_COMPAT, 'utf-8');
		return $value;
	}
	public static function htmlAttrName($attr) {
		$attr = preg_replace('/[^:_a-zA-Z-]/','_',$attr);
	}
	public static function javascript() {}
	public static function filename($path) {
		// keep only whitelisted characters, replacing others with an underscore
		// and prevent two consecutive underscores
		$path = preg_replace('/[^a-zA-Z0-9~\(\).-]+/', '_', $path);
		$path = rtrim($path, '.'); // windows can't handle a trailing dot
		return $path;		
	}
	public static function path($path) {
		$prefix = '';
		// handle file and svn type protocols?
		if (preg_match('/^[a-zA-Z]\:\//', $path)) {
			$prefix = substr($path, 0, 3);
			$path = substr($path, 3);
		}
		// keep only whitelisted characters, replacing others with an underscore
		// and prevent two consecutive underscores
		$path = preg_replace('/[^a-zA-Z0-9~\(\).\/-]+/', '_', $path);
		$path = rtrim($path, '.'); // windows can't handle a trailing dot
		$path = $prefix . $path;
		return $path;		
	}	
	public static function htmlId($name) {
		// Keep only whitelisted characters, replacing others with an underscore
		//   and prevent two consecutive underscores.
		// Colons and periods are also allowed by spec, but screw up some browsers
		$name = preg_replace('/[^\w_-]+/', '-', $name);
		// don't begin or end with hyphen or
		$name = trim($name,'-');
		// ensure it begins with a letter
		if (!preg_match('/^[a-zA-Z]/', $name)) {
			$name = "id-$name";
		}	
		return $name;		
	}
	public static function htmlClass() {}

}